﻿using UnityEngine;
using System.Collections;

public class savepic : MonoBehaviour {

  	// Use this for initialization
	void Start () {

        var tex = GetComponent<Camera>().targetTexture;
        var text2d = TakePicture(tex);
        System.IO.File.WriteAllBytes(Application.dataPath + "/a.png", text2d.EncodeToPNG());
	}
    Texture2D TakePicture(RenderTexture source)
    {
        // TODO: Experiment with Compute Shaders, it may be faster to return data from a ComputeBuffer
        // than ReadPixels
        Texture2D target = new Texture2D((int)source.width, (int)source.height, TextureFormat.ARGB32, false);
        target.hideFlags = HideFlags.HideAndDontSave;
        target.wrapMode = TextureWrapMode.Clamp;
        target.filterMode = FilterMode.Bilinear;
        target.anisoLevel = 0;

        RenderTexture.active = source;

        target.ReadPixels(new Rect(0, 0, source.width, source.height), 0, 0);
        target.Apply();
        RenderTexture.active = null;

        return target;
    }
	// Update is called once per frame
	void Update () {
	
	}
}
